package com.songlanyun.modules.chain.controller;

import com.songlanyun.common.annotation.AuthIgnore;
import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.enums.ChainConstant;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.chain.model.dto.ChainNoticeRechargeDTO;
import com.songlanyun.modules.chain.model.dto.ChainNoticeWithdrawalDTO;
import com.songlanyun.modules.chain.service.ChainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/chain")
@Slf4j
public class ChainController {

    @Resource
    ChainService chainService;

    /**
     * 代币充值成功通知
     *
     * @param chainNoticeRechargeDTO 代币充值成功通知入参
     * @return R 代币充值成功通知出参
     */
    @PostMapping(value = "/notice/recharge")
    @RepeatSubmit(value = "3")
    @AuthIgnore
    public R notcieRecharge(@RequestBody ChainNoticeRechargeDTO chainNoticeRechargeDTO) {
        log.info("代币转入回调入参：{}", chainNoticeRechargeDTO);
        ValidatorUtils.validateEntity(chainNoticeRechargeDTO);
        chainService.recharge(chainNoticeRechargeDTO);
        return R.ok("转入成功！");
    }

    /**
     * 积分提现成功通知
     *
     * @param noticeRechargeDTO 积分提现成功通知入参
     * @return R 币提现成功通知出参
     */
    @PostMapping(value = "/notice/withdrawal")
    @RepeatSubmit(value = "3")
    @AuthIgnore
    public R notcieWithdrawal(@RequestBody ChainNoticeWithdrawalDTO noticeRechargeDTO) {
        log.info("积分提现成功入参：{}", noticeRechargeDTO);
        ValidatorUtils.validateEntity(noticeRechargeDTO);
        chainService.withdrawalNotice(noticeRechargeDTO);
        return R.ok(ChainConstant.ChainBsStatus.WITHDRAW_FAIL.equals(noticeRechargeDTO.getBsStatus()) ? "积分提现失败，返还用户积分成功！" : "积分提现成功！");
    }

}
